#!/bin/bash

#安装Nova
yum install -y openstack-nova-compute
#安装Neutron
yum install -y openstack-neutron-linuxbridge ebtables ipset

cp /etc/nova/nova.conf{,.compute}
cp /etc/neutron/neutron.conf{,.compute}
cp /etc/neutron/plugins/ml2/linuxbridge_agent.ini{,.compute}

Vdir=/data/nova
VHD=$Vdir/instances
mkdir -p $VHD
chown -R nova:nova $Vdir

#Netname=eth1  #网卡名
read -p "input ethernet name:" Netname
if [ -z $Netname ]
then
  Netname=eth1
fi

IP=`ifconfig $Netname | grep 'inet' | grep -v 'inet6' | cut -d: -f2 | awk '{ print $2}'`
echo $Netname
echo $IP

#使用QEMU或KVM ,KVM硬件加速需要硬件支持,虚拟机使用 qemu
[[ `egrep -c '(vmx|svm)' /proc/cpuinfo` = 0 ]] && { Kvm=qemu; } || { Kvm=kvm; }
echo "使用 $Kvm"

VncProxy=192.168.100.99 #VNC代理地址vip

# #nova配置
novafile=/etc/nova/nova.conf

openstack-config --set $novafile DEFAULT instances_path $VHD
openstack-config --set $novafile DEFAULT enabled_apis osapi_compute,metadata
openstack-config --set $novafile DEFAULT transport_url rabbit://openstack:openstack@controller:5673
openstack-config --set $novafile DEFAULT my_ip $IP
openstack-config --set $novafile DEFAULT use_neutron True
openstack-config --set $novafile DEFAULT firewall_driver nova.virt.firewall.NoopFirewallDriver
openstack-config --set $novafile DEFAULT cpu_allocation_ratio 8

openstack-config --set $novafile api_database connection mysql+pymysql://nova:nova@controller/nova_api

openstack-config --set $novafile database connection mysql+pymysql://nova:nova@controller/nova

openstack-config --set $novafile api auth_strategy keystone

openstack-config --set $novafile keystone_authtoken auth_uri http://controller:5000
openstack-config --set $novafile keystone_authtoken auth_url http://controller:35357
openstack-config --set $novafile keystone_authtoken memcached_servers controller1:11211,controller2:11211,controller3:11211
openstack-config --set $novafile keystone_authtoken auth_type password
openstack-config --set $novafile keystone_authtoken project_domain_name default
openstack-config --set $novafile keystone_authtoken user_domain_name default
openstack-config --set $novafile keystone_authtoken project_name service
openstack-config --set $novafile keystone_authtoken username nova
openstack-config --set $novafile keystone_authtoken password nova

openstack-config --set $novafile vnc enabled true
openstack-config --set $novafile vnc vncserver_listen 0.0.0.0
openstack-config --set $novafile vnc vncserver_proxyclient_address '$my_ip'
openstack-config --set $novafile vnc novncproxy_base_url http://$VncProxy:6080/vnc_auto.html

openstack-config --set $novafile glance api_servers http://controller:9292

openstack-config --set $novafile oslo_concurrency lock_path /var/lib/nova/tmp

openstack-config --set $novafile placement os_region_name RegionOne
openstack-config --set $novafile placement project_domain_name Default
openstack-config --set $novafile placement project_name service
openstack-config --set $novafile placement auth_type password
openstack-config --set $novafile placement user_domain_name Default
openstack-config --set $novafile placement auth_url http://controller:35357/v3
openstack-config --set $novafile placement username placement
openstack-config --set $novafile placement password placement

openstack-config --set $novafile libvirt virt_type $Kvm
openstack-config --set $novafile libvirt cpu_mode host-passthrough

openstack-config --set $novafile neutron url http://controller:9696
openstack-config --set $novafile neutron auth_url http://controller:35357
openstack-config --set $novafile neutron auth_type password
openstack-config --set $novafile neutron project_domain_name default
openstack-config --set $novafile neutron user_domain_name default
openstack-config --set $novafile neutron region_name RegionOne
openstack-config --set $novafile neutron project_name service
openstack-config --set $novafile neutron username neutron
openstack-config --set $novafile neutron password neutron

#-----------------------------------------
#neutron配置
neutronfile=/etc/neutron/neutron.conf

openstack-config --set $neutronfile DEFAULT auth_strategy keystone
openstack-config --set $neutronfile DEFAULT transport_url rabbit://openstack:openstack@controller:5673

openstack-config --set $neutronfile keystone_authtoken auth_uri http://controller:5000
openstack-config --set $neutronfile keystone_authtoken auth_url http://controller:35357
openstack-config --set $neutronfile keystone_authtoken memcached_servers controller1:11211,controller2:11211,controller3:11211
openstack-config --set $neutronfile keystone_authtoken auth_type password
openstack-config --set $neutronfile keystone_authtoken project_domain_id default
openstack-config --set $neutronfile keystone_authtoken user_domain_id default
openstack-config --set $neutronfile keystone_authtoken project_name service
openstack-config --set $neutronfile keystone_authtoken username neutron
openstack-config --set $neutronfile keystone_authtoken password neutron

openstack-config --set $neutronfile oslo_concurrency lock_path /var/lib/neutron/tmp

# linuxbridge_agent.ini
# provider:网卡名
settingfile=/etc/neutron/plugins/ml2/linuxbridge_agent.ini

openstack-config --set $settingfile linux_bridge physical_interface_mappings provider:$Netname

openstack-config --set $settingfile enable_security_group true
openstack-config --set $settingfile firewall_driver neutron.agent.linux.iptables_firewall.IptablesFirewallDriver

openstack-config --set $settingfile vxlan enable_vxlan false

#启动
systemctl enable libvirtd.service openstack-nova-compute.service neutron-linuxbridge-agent.service
systemctl restart libvirtd.service openstack-nova-compute.service neutron-linuxbridge-agent.service
#
nova-manage cell_v2 discover_hosts
openstack compute service list
